<title>涉农纳税人基础信息</title>


<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-agriculture" data-type="batchdel">删除</button>
                <button class="layui-btn layuiadmin-btn-agriculture" data-type="add">添加</button>
                <button type="button" class="layui-btn" id="agriculture" style="margin-right: 10px">批量添加</button>
                <button class="layui-btn layuiadmin-btn-agriculture" data-type="save">保存</button>
                <button class="layui-btn layuiadmin-btn-agriculture" lay-filter="LAY-agriculture-front-search"
                        data-type="reload"
                >
                    <!--                    <i class="layui-icon  layuiadmin-button-btn">&#xe669;</i>-->
                    刷新
                </button>
                <!--                <button class="layui-btn layuiadmin-btn-agriculture" data-type="reload"-->
                <!--                        style="float: right;margin: 0">-->
                <!--                    <i class="layui-icon ">&#xe669;</i>-->
                <!--                </button>-->
                <button class="layui-btn layuiadmin-btn-agriculture" lay-filter="LAY-agriculture-front-search"
                        data-type="reload"
                        style="float: right;margin: 0">
                    <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                </button>
                <input onfocus="" style="width: 200px; float: right" type="text"
                       name="agricultureinfo"
                       value="" placeholder="模糊搜索" autocomplete="off" class="layui-form layui-input">

            </div>

            <table id="Agricultural-Industry" lay-filter="Agricultural-Industry"></table>
        </div>
    </div>
</div>

<script>
    layui.use(['form', 'table', 'admin', 'view', 'upload'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table
            , admin = layui.admin
            , view = layui.view
            , upload = layui.upload
        upload.render({
            elem: '#agriculture'
            , url: 'http://127.0.0.1:8000/receive_file/'
            , accept: 'file' //普通文件
            , done: function (res) {
                // console.log(res)
            }
        });
        table.render({
            elem: '#Agricultural-Industry'
            , url: './json/basicinformation/agriculture.json?keyword=aaa' //模拟接口
            // , id: 'AgriculturalIndustry'
            , toolbar: '#agricultural-tool'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'NSRSBH', width: 120, title: '纳税人识别号',  align: 'center'}
                , {field: 'NSRMC', width: 100, title: '纳税人名称', edit: 'text', align: 'center'}
                , {field: 'TDMJ', title: '土地面积', edit: 'text', align: 'center'}
                , {field: 'ZZYZPZ', title: '种植(养殖)品种', edit: 'text', align: 'center'}
                , {field: 'SZZQ', title: '生长周期', edit: 'text', align: 'center'}
                , {field: 'ZZRQ', title: '种植日期', edit: 'text', align: 'center'}
                , {field: 'YXBZ', title: '有效标志', edit: 'text', align: 'center'}
                , {field: 'LRR_DM', title: '录入人代码', align: 'center'}
                , {field: 'LRRQ', title: '录入日期', align: 'center'}
                , {field: 'XGR_DM', title: '修改人代码',  align: 'center'}
                , {field: 'XGRQ', title: '修改日期', align: 'center'}
            ]]
            // ,page: true
            , page: true
            , height: 'full-320'
            , limit: 1
            , text: '对不起，加载出现异常！'
        });

        var editlis = []
        table.on('edit(Agricultural-Industry)', function (obj) {
            var value = obj.value //得到修改后的值
                , data = obj.data //得到所在行所有键值
                , field = obj.field//得到字段
                , a = true; // 局部变量判断
            if (editlis.length === 0) {
                editlis.push(data)
            }
            for (let i = 0; i < editlis.length; i++) {
                if (data.NSRSBH === editlis[i].NSRSBH) {
                    a = false
                    editlis[i] = data
                }
            }
            if (a) {
                editlis.push(data)
            }
        });


        var active = {
            batchdel: function () {
                var checkStatus = table.checkStatus('Agricultural-Industry')
                    , checkData = checkStatus.data  //得到选中的数据
                    , delobjs = []
                ;
                debugger
                for (let i = 0; i < checkData.length; i++) {
                    delobjs.push(checkData[i].roleId)
                }
                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }

                layer.confirm('确定删除吗？', function (index) {
                    //执行 Ajax 后重载
                    $.ajax({
                        url: `http://10.0.23.97:8195/portal/role/role`,
                        dataType: 'json',
                        data: JSON.stringify(delobjs),
                        async: false,
                        beforeSend: function (XMLHttpRequest) {
                            XMLHttpRequest.setRequestHeader("Authorization", layui.data('layuiAdmin').Authorization);
                            XMLHttpRequest.setRequestHeader("Content-Type", "application/json");
                        },
                        type: 'DELETE',
                        success: function (res) {
                            // console.log("删除结果====" + JSON.stringify(res))
                            doSuccess();
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            //通常情况下textStatus和errorThrown只有其中一个包含信息
                            doFail();
                            // console.log(textStatus);   //调用本次ajax请求时传递的options参数
                            // console.log(errorThrown);   //调用本次ajax请求时传递的options参数
                        }

                    })
                    table.reload('Agricultural-Industry');

                });
            },
            add: function () {
                admin.popup({
                    title: '添加涉农纳税人基础信息'
                    , area: ['600px', '700px']
                    // , id: 'LAY-popup-user-add'
                    , success: function (layero, index) {
                        view(this.id).render('DataAnalysisWarning/BasicInformationCollection/agricultural/addAgriculture').done(function () {
                            form.render(null, 'layuiadmin-form-agriculture');
                            //监听提交
                            form.on('submit(LAY-agriculture-submit)', function (data) {
                                var field = data.field; //获取提交的字段
                                // console.log("权限数据===" + JSON.stringify(permission))
                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                $.ajax({
                                    url: "http://10.0.23.97:8195/portal/role/role",
                                    data: field,
                                    dataType: "json",
                                    async: false,
                                    beforeSend: function (XMLHttpRequest) {
                                        XMLHttpRequest.setRequestHeader("Authorization", layui.data('layuiAdmin').Authorization);
                                    },
                                    type: 'POST',
                                    success: function (res) {
                                        doSuccess()
                                    },
                                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                                        //通常情况下textStatus和errorThrown只有其中一个包含信息
                                        doFail()
                                    }
                                })
                                table.reload('Agricultural-Industry'); //重载表格
                                layer.close(index); //执行关闭
                            });
                        });
                    }
                });
            },
            reload: function () {
                // var roleVal = $("input[name='roleinfo']").val()
                //执行重载
                table.reload('Agricultural-Industry');
            },
            save: function () {
                alert(JSON.stringify(editlis))
                doSuccess()


                // $.ajax({
                //     url: `http://10.0.23.97:8195/portal/role/role`,
                //     dataType: 'json',
                //     data: JSON.stringify(delobjs),
                //     async: false,
                //     beforeSend: function (XMLHttpRequest) {
                //         XMLHttpRequest.setRequestHeader("Authorization", layui.data('layuiAdmin').Authorization);
                //         XMLHttpRequest.setRequestHeader("Content-Type", "application/json");
                //     },
                //     type: 'DELETE',
                //     success: function (res) {
                //         console.log("删除结果====" + JSON.stringify(res))
                //         doSuccess();
                //     },
                //     error: function (XMLHttpRequest, textStatus, errorThrown) {
                //         //通常情况下textStatus和errorThrown只有其中一个包含信息
                //         doFail();
                //         console.log(textStatus);   //调用本次ajax请求时传递的options参数
                //         console.log(errorThrown);   //调用本次ajax请求时传递的options参数
                //     }
                //
                // })
                table.reload('Agricultural-Industry');
            }
        }
        $('.layui-btn.layuiadmin-btn-agriculture').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    })

</script>